package com.snowleopard.findpeople.web.account;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.snowleopard.findpeople.entity.account.User;
import com.snowleopard.findpeople.service.account.AccountManager;
import com.snowleopard.findpeople.web.CrudActionSupport;

@Namespace("/account")
@Results({ @Result(name = "home", location = "/login.action", type = "redirect") })
public class ForgotPasswordAction extends CrudActionSupport<User> {

    private static final long serialVersionUID = 3012095312586461232L;
    private static final Logger log = LoggerFactory.getLogger(ForgotPasswordAction.class);

    @Autowired
    private AccountManager accountManager;

    private User user;
    private String loginId;
    private String answer;
    private String id;
    private String password;

    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String list() throws Exception {
        // TODO Auto-generated method stub
        return SUCCESS;
    }

    public void check() throws Exception {
        log.info("ID=" + loginId + " answer:" + answer);
        User dbUser = accountManager.getUser(loginId);
        log.info("DB answer:" + dbUser.getAnswer());
        if (null != dbUser && answer.equals(dbUser.getAnswer()))
            Struts2Utils.renderHtml("success," + dbUser.getId());
        else
            Struts2Utils.renderHtml("failed");
    }

    public void validateUserExist() {
        user = accountManager.findUserByLoginName(loginId);
        if (null != user) {
            Struts2Utils.renderHtml("success," + user.getQuestion() + "," + user.getId());
        } else {
            Struts2Utils.renderHtml("failed");
        }
    }

    @Override
    public String input() throws Exception {
        Struts2Utils.renderHtml("SUCCESS");
        return SUCCESS;
    }

    @Override
    public String save() throws Exception {
        log.info("ID:" + id);
        User dbUser = accountManager.getUser(id);
        dbUser.setPassword(password);
        accountManager.updateUser(dbUser);
        return "home";
    }

    @Override
    public String delete() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getLoginId() {
        return loginId;
    }

    public void setLoginId(String loginId) {
        this.loginId = loginId;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
